package com.icexxx.ice_notepad;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JTextArea;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.core.util.StrUtil;

public class ButtonManager {

    private static String path;

    public static String getPath() {
        return path;
    }

    public static void setPath(String path) {
        if (StrUtil.isNotBlank(path)) {
            path = path.replace("\\", "/");
            ButtonManager.path = path;
        }
    }

    public static Component createPasteButton(JTextArea jTextArea) {
        JButton jButton = new JButton("粘贴");
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    String str = ClipboardUtil.getStr();
                    str = StringUtil.filterText(str);
                    jTextArea.setText(str);
                }
            }
        };
        jButton.addMouseListener(mouseListener);
        return jButton;
    }

    public static Component createCopyButton(JTextArea jTextArea) {
        JButton jButton = new JButton("复制");
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    String str = jTextArea.getText();
                    str = StringUtil.filterText(str);
                    ClipboardUtil.setStr(str);
                }
            }
        };
        jButton.addMouseListener(mouseListener);
        return jButton;
    }

    public static Component createEmptyButton(JTextArea jTextArea) {
        JButton jButton = new JButton("清空");
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    jTextArea.setText("");
                }
            }
        };
        jButton.addMouseListener(mouseListener);
        return jButton;
    }

    public static Component createSaveButton(JTextArea jTextArea, String title) {
        JButton jButton = new JButton("保存");
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    String content = jTextArea.getText();
                    content = content.replaceAll("([^\\r])(\\n)", "$1\r\n");
                    String now = DateUtil.format(new Date(), "yyyy-MM-dd_HHmmss");
                    String month = DateUtil.format(new Date(), "yyyyMM");
                    String date = DateUtil.format(new Date(), "yyyyMMdd");
                    if (StrUtil.isBlank(path)) {
                        MessageBox.message("文件保存路径不能为空");
                    } else {
                        path = StrUtil.removeSuffix(path, "/");
                        String fileName = path + "/" + title + "/" + month + "/" + date + "/" + now + ".txt";
                        FileUtil.writeUtf8String(content, fileName);
                    }
                }
            }
        };
        jButton.addMouseListener(mouseListener);
        return jButton;
    }

    public static Component createLoadButton(JTextArea jTextArea, String title) {
        JButton jButton = new JButton("加载");
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    if (StrUtil.isBlank(path)) {
                        MessageBox.message("文件保存路径不能为空");
                    } else {
                        path = StrUtil.removeSuffix(path, "/");
                        String fileName = path + "/" + title;
                        List<File> list = FileUtil.loopFiles(new File(fileName), 1, new FileFilter() {
                            @Override
                            public boolean accept(File pathname) {
                                return pathname.isDirectory() && pathname.getName().length() == 6;
                            }
                        });
                        String max = null;
                        if (list.isEmpty()) {
                            MessageBox.message("文件夹没有数据");
                        } else {
                            max = list.stream().map(f -> f.getName()).max(String::compareTo).get();
                            fileName = fileName + "/" + max;
                            list = FileUtil.loopFiles(new File(fileName), 1, new FileFilter() {
                                @Override
                                public boolean accept(File pathname) {
                                    return pathname.isDirectory() && pathname.getName().length() == 8;
                                }
                            });
                            if (list.isEmpty()) {
                                MessageBox.message("文件夹没有数据");
                            } else {
                                max = list.stream().map(f -> f.getName()).max(String::compareTo).get();
                                fileName = fileName + "/" + max;
                                list = FileUtil.loopFiles(new File(fileName), 1, new FileFilter() {
                                    @Override
                                    public boolean accept(File pathname) {
                                        return pathname.isFile() && pathname.getName().endsWith(".txt");
                                    }
                                });
                                if (list.isEmpty()) {
                                    MessageBox.message("文件夹没有数据");
                                } else {
                                    max = list.stream().map(f -> f.getName()).max(String::compareTo).get();
                                    fileName = fileName + "/" + max;
                                    String content = FileUtil.readUtf8String(fileName);
                                    content = StringUtil.filterText(content);
                                    jTextArea.setText(content);
                                }
                            }
                        }
                    }
                }
            }
        };
        jButton.addMouseListener(mouseListener);
        return jButton;
    }

}
